#pragma once
#include <ctime>
#include <cstdarg>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <iostream>
#include <sys/socket.h>
#include <cstring>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>

//日志
#define LOG_LEVEL_DEBUG "DEBUG"
#define LOG_LEVEL_INFO  "INFO"
#define LOG_LEVEL_ERROR "ERROR"

const char* Time()
{
    // NULL表示获取当前时间戳
    time_t now = time(NULL); 
    // 转换为本地时间的struct tm
    struct tm* tm_now = localtime(&now);
    //格式版
    static char buff[32]={0};
    strftime(buff, sizeof(buff), "%Y-%m-%d %H:%M:%S", tm_now);
    
    return buff;
}

//日志核心输出                 等级             文件名   行号             错误原因(用可变参数可以完整的打印错误信息)
void log_message(const char* level,const char* file,int line,const char* because...)
{
    // 存储格式化后的错误原因
    char msg_buf[1024] = {0};
 
    //存储可变参数
    va_list s;
    //初始化参数列表
    va_start(s,because);
 
    //将核心信息统一存到数组
    vsnprintf(msg_buf, sizeof(msg_buf), because, s);
 
    //为什么没有用Va_arg：通过 vsnprintf 等函数间接处理时：类型由 “格式字符串” 隐式指定
 
    //清理列表
    va_end(s);
 
    std::cout<<"等级:"<<level<<"  "<<"时间:"<<Time()<<"  "<<"文件名:"<<file<<"  "<<"行号:"<<line<<"  "<<msg_buf<<std::endl;
}

//网络通信
class Network_Communicat
{
public:
    Network_Communicat()
    {}
    
    //定义套接字（端口号设置为0表示需要OS自己分配）
    void Init()
    {
        //定义端口
        _fd = socket(AF_INET,SOCK_DGRAM,0);
        if(_fd==-1)
        {
            log_message(LOG_LEVEL_ERROR,__FILE__,__LINE__,"错误码：%d,错误原因：%s",errno,strerror(errno));
            exit(0);
        }
        else
        {
            log_message(LOG_LEVEL_ERROR,__FILE__,__LINE__,"socket is successful!");
        }
        //绑定地址和端口
        struct sockaddr_in local;
        local.sin_family=AF_INET;
        local.sin_addr.s_addr = inet_addr("0.0.0.0");
        local.sin_port=htons(8080);
        //memset(&local.sin_zero, 0, sizeof(local.sin_zero));

        int bt = bind(_fd,(const sockaddr*)&local,sizeof(local));
        if(bt==-1)
        {
            log_message(LOG_LEVEL_ERROR,__FILE__,__LINE__,"错误码：%d,错误原因：%s",errno,strerror(errno));
            exit(0);
        }
        else
        {
            log_message(LOG_LEVEL_ERROR,__FILE__,__LINE__,"bind is successful!");
        }
    }

    //接收数据
    void Accept()
    {
        //存储数据
        char buffer[1024]={'0'};
        struct sockaddr_in local_accept; 
        // local_accept.sin_family = AF_INET;       
        // local_accept.sin_port = htons(8080);      
        // local_accept.sin_addr.s_addr = INADDR_ANY;
        socklen_t addr_len = sizeof(local_accept);
        size_t re = recvfrom(_fd,buffer,sizeof(buffer),0,(struct sockaddr*)&local_accept,&addr_len);
        while(1)
        {
            if(re==-1)
            {
                log_message(LOG_LEVEL_ERROR,__FILE__,__LINE__,"错误码：%d,错误原因：%s",errno,strerror(errno));
                sleep(1);
            }
            else
            {
                std::cout<<"接收成功!收到内容:"<<buffer<<std::endl;
                break;
            }
        }
    }

private:
    //套接字文件fd
    int _fd=0;
};